package com.ccsu.joysport.service;

import com.ccsu.joysport.entity.Info;
import com.ccsu.joysport.repository.setInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class setInfoService {
    @Autowired
    private setInfoRepository setInfoRepository;

    public List<Info> getAllNews() {
        return setInfoRepository.findAllByOrderBySortOrderAsc();
    }

    public Info addNews(Info news) {
        return setInfoRepository.save(news);
    }

    public void deleteNews(Long id) {
        setInfoRepository.deleteById(id);
    }

    public Info updateNews(Long id, Info updatedNews) {
        return setInfoRepository.findById(id)
                .map(news -> {
                    news.setTitle(updatedNews.getTitle());
                    news.setDescription(updatedNews.getDescription());
                    news.setImageUrl(updatedNews.getImageUrl());
                    news.setUrl(updatedNews.getUrl());
                    news.setSortOrder(updatedNews.getSortOrder());
                    return setInfoRepository.save(news);
                })
                .orElseThrow(() -> new RuntimeException("News not found"));
    }
}
